package com.example.boke.controller;
/**
 * author 依赖
 * <p>
 * ./src/main/resources/upload
 */

import com.example.boke.entity.User;
import com.example.boke.mapper.UserMapper;
import com.example.boke.tool.LocalDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/user/")
public class UserController
{
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("reg") //注册代码
    public String reg(User user) throws IOException
    {
        /*//        System.out.println("user = " + user + ", picFile = " + picFile);
         *//*User u =mapper.check(user.getUsername());*//*
        //获取原文件名
        String fileName = picFile.getOriginalFilename();
        //获取文件名后缀
        String suff = fileName.substring(fileName.lastIndexOf("."));
        //改成唯一的文件名
        fileName = UUID.randomUUID() + suff;
        //创建文件夹
        //设置路径
        String dirPath = LocalDir.dir() + "/user";
        //判断文件是否存在
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        String filePath = dirPath + "/" + fileName;
        picFile.transferTo(new File(filePath));

        //把图片的绝对路径添加到User对象中
        user.setUrl("/user/" + fileName);*/
        System.out.println(user);
        mapper.insert(user);
        return "注册成功!";
    }

    @RequestMapping("login") //登录代码
    public int login(User user, HttpSession session)
    {
//      System.out.println("user = " + user + ", session = " + session);

        User u = mapper.check(user.getUsername());
        if (u != null)
        {
            if (user.getPassword().equals(u.getPassword()))
            {
                //把查询到的用户对象 保存到session对话对象中
                session.setAttribute("user", u);
                System.out.println("id:" + u.getId());
                return 1;//登录成功

            }
            else
            {
                return 3;//密码错误
            }
        }
        return 2; //用户不存在
    }

    @RequestMapping("logout")
    public void logout(HttpSession session)
    {
        //删除登录成功时保存用户的对象
        session.removeAttribute("user");
    }

    @RequestMapping("logoff") //用户注销功能
    public void logoff(HttpSession session)
    {
        System.out.println("session = " + session);
        User user = (User) session.getAttribute("user");
        System.out.println("user= " + user);
        /*String username = user.getUsername();
        String password = user.getPassword();
        System.out.println(username);
        System.out.println(password);

        Integer id = mapper.selectId(username, password);
        System.out.println(id);*/
        Integer id = user.getId();
        System.out.println("ID:" + id);
        mapper.deleteLogoff(id);
    }

    @RequestMapping("update")
    public void updateUser(User user, HttpSession session)
    {
        System.out.println("session = " + session);
        User u = (User) session.getAttribute("user");
        /*String username = user.getUsername();
        String password = user.getPassword();
        System.out.println(username+"-----------");
        System.out.println(password+"-----------");

        Integer id= mapper.selectId(username,password);
        System.out.println(id);
*/
        Integer id = u.getId();
        System.out.println("ID:" + id);
        user.setId(id);
        mapper.update(user);
    }


}
